<%-- 
    Document   : orderDetail
    Created on : Apr 21, 2013, 3:49:51 AM
    Author     : Andriy, Tkach
--%>

<%@page import="entity.Users"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="facade.CommentsFacadeLocal"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="entity.ProductList"%>
<%@page import="entity.Orders"%>
<%@page import="entity.Comments"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%!    private CommentsFacadeLocal commentsFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();

            commentsFacade = (CommentsFacadeLocal) ic.lookup(
                    "java:global/Delta/Delta-ejb/CommentsFacade!facade.CommentsFacadeLocal");

        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>
<!DOCTYPE html>

<script>
    function validateComment() {
        var valid = true;
        var description = document.getElementById("textarea").value;
        if (description.length === 0 || description.length > 600) {
            document.getElementById("comment_error").style.display = "inline";
            document.getElementById("add_button").disabled = true;
            valid = false;
        } else {
            document.getElementById("comment_error").style.display = "none";
            document.getElementById("add_button").disabled = false;
        }
        return valid;
    }

    function addComment() {
        if (validateComment()) {
            document.getElementById("comments").action = "addComment";
            document.getElementById("dashboard").value = "<%=request.getAttribute("dashboard")%>";
            document.getElementById("comments").submit();
        }
    }
</script>
<%
    Users user = (Users) session.getAttribute("user");
%>
<html>    
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/courier.css">
        <link rel="stylesheet" type="text/css" href="css/all.css">
        <title>Order Detail</title>
    </head>
    <body>
        <header>
            <div id ="top_name"> <a href="main.jsp"> <img src="/Delta-war/product/1.jpeg"></a> <h1> Order Detail</h1> </div>
            
            <div id="registr2">
                <p> <a href="user_name.jsp"><%=user.getLogin()%></a>
                    <a href="logout">LogOut</a> </p>
            </div>

        </header>
        <div id="main">
            <div id="all">
                <% if ((Integer) request.getAttribute("dashboard") == 2) {%><a href="WarehouseOrders">To ALL</a><%}%>
                <% if ((Integer) request.getAttribute("dashboard") == 1) {%><a href="courier">To ALL</a><%}%>
            </div>
            <div id="tabl2" >
                <% if (request.getAttribute("error") == null) {
                        Orders order = (Orders) request.getAttribute("order");%>
                <table width="50%">

                    <TR>
                        <td id="head_table_first" >Order </td>
                        <td id="colomn_first" > <%=order.getTrackingNumber()%></td> 
                    </TR>
                    <TR>
                        <td id="head_table_first">Order Date </td>
                        <%SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");%>
                        <td id="colomn_first" > <%=sdf.format(order.getOrderDate())%></td> 
                    </TR>

                    <TR>
                        <td id="head_table_first">  Order Status</td>
                        <td id="colomn_first" > <%=order.getOrderStatus()%></td> 
                    </TR>
                    <%if (order.getDeliveryDate() != null && order.getOrderStatus().equalsIgnoreCase("delivered")) {%>
                    <TR>
                        <td id="head_table_first">Delivery Date </td>
                        <td id="colomn_first" > <%=sdf.format(order.getDeliveryDate())%></td> 
                    </TR><%}%>
                    <TR>
                        <td id="head_table_first">Warehouse </td>
                        <td id="colomn_first" > <%=order.getIdWarehouse().getName()%>,<%=order.getIdWarehouse().getAddress()%></td> 
                    </TR>

                    <TR>
                        <td id="head_table_first">Address </td>
                        <td id="colomn_first" > <%=order.getAddress()%></td>
                    </TR>

                    <TR>
                        <td id="head_table_first">  Shipping price</td>
                        <td id="colomn_first" > <%=order.getShippingPrice()%></td>
                    </TR>

                    <TR>
                        <td id="head_table_first">  Product price</td>
                        <td id="colomn_first" > <%=order.getProductPrice()%></td>
                    </TR>

                    <TR>
                        <td id="head_table_first"> Total price</td>
                        <td id="colomn_first" > <%=order.getProductPrice().add(order.getShippingPrice())%></td>
                    </TR>
                    <TR>
                        <td id="head_table_first">  User</td>
                        <td id="colomn_first" > <%if (order.getIdUser() != null) {%><%=order.getIdUser().getLastName()%>,<%=order.getIdUser().getFirstName()%> <% } else {%>Unregistered<%}%></td>
                    </TR>
                    <TR>
                        <td id="head_table_first">  Email</td>
                        <td id="colomn_first" > <%=order.getEMail()%></td>
                    </TR>
                </table> 

                <div id="tabl3">
                    <table>
                        <TR>
                            <td id="head_table_first" >Product name </td>
                            <td id="head_table_first" >Price for one </td>
                            <td id="head_table_first" >Count </td>
                        </TR>
                        <%for (Iterator<ProductList> itr = order.getProductListCollection().iterator(); itr.hasNext();) {
                                ProductList pl = itr.next();%>
                        <TR>
                            <td id="colomn_first"><%=pl.getProduct().getName()%></td>
                            <td id="colomn_first"><%=pl.getProduct().getPrice()%></td>
                            <td id="colomn_first"><%=pl.getCount()%></td>
                        </TR>
                        <% }%>
                    </table>
                </div>
                <div id="set">
                    <% if ((Integer) request.getAttribute("dashboard") == 2) {
                            if (order.getOrderStatus().equalsIgnoreCase("queued")) {%>
                    <form name="cancel" method="POST" action="CancelOrder">

                        <input type="hidden" name="idOrder" value="<%=order.getIdOrder()%>"/>
                        <input type="hidden" name="orderTrackNum" value="<%=order.getTrackingNumber()%>"/>
                        <input type="submit" value="Cancel"/> 
                    </form>
                    <form name="setShipped" method="POST" action="SetShipped">
                        <input type="hidden" name="idOrder" value="<%=order.getIdOrder()%>"/>
                        <input type="hidden" name="orderTrackNum" value="<%=order.getTrackingNumber()%>"/>
                        <input type="submit" value="Set shipped"/> 
                    </form><%}
                        }%>
                    <% if ((Integer) request.getAttribute("dashboard") == 1 && order.getOrderStatus().equalsIgnoreCase("shipped")) {%>    
                    <form id="setDeliuvered" method="POST" action="courier">
                        <input type="hidden" name="orderTrackNum" value="<%=order.getTrackingNumber()%>"/>
                        <input type="hidden" name="idOrder" value="<%=order.getIdOrder()%>"/>
                        <input type="submit"  value="Set delivered"/> 
                    </form>
                    <%}%>
                    <%if (request.getAttribute("success") != null) {%>
                    <script type=text/javascript>
                        alert("<%=request.getAttribute("success")%>");
                    </script>
                    <%}%>


                    <%} else {%>You can't do this. <%}%>
                </div>
            </div>

        </div>
        <div id="comm">
            <form method="post" action="addComment" id="comments">
                <input type="hidden"  id="dashboard" name="dashboard" value="<%=request.getAttribute("dashboard")%>" />
                <table width="446" border="2" >
                    <TR>
                        <td colspan="3" id="head_table_first" >Comments</td>
                    </TR>
                    <%
                        Orders order = (Orders) request.getAttribute("order");
                        //ArrayList<Comments> listOfComments = new ArrayList<Comments>();
                        for (Comments comments : commentsFacade.findByOrder(order)) {
                            //   listOfComments.add(comments);
                            //}
                            // for (Comments comments : listOfComments) {
%>
                    <%SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");%>
                    <TR >
                    <font color="GREEN">
                    <td width="100" id="without_border" >
                        <%=comments.getIdUser().getLogin()%>
                    </td>
                    <td  id="without_border">
                        <%=comments.getText()%>
                    </td>
                    <td width="125" id="without_border" >
                        <%=sdf.format(comments.getCommentDate())%>
                    </td>
                    </font>
                    </TR>


                    <% }
                        // }%>
                    <input type="hidden" name="idOrder" value="<%=order.getIdOrder()%>"/>
                    <TR>
                        <td colspan="3" id="without_border">&nbsp;</td>
                    </TR>
                    <TR><td colspan="3">
                            <span 
                                id="comment_error" 
                                style="color: red; display:none"> 
                                Comment's length must be from 1 to 600 symbols. 
                            </span>
                        </td>
                    </TR>

                    <TR >
                        <td colspan="2"  id="without_border"><label for="add_comment_field"></label>
                            <label for="textarea"></label>
                            <textarea name="textarea" id="textarea"  cols="35" rows="3" placeholder="Enter comment here" oninput="validateComment()"></textarea></td>
                        <td  id="without_border">
                            <button id="add_button" disabled="true" value="Add" onclick="addComment()">Add comment

                            </button></td>
                    </TR>

                </table>
            </form>

        </div>
<div id="footer2">
                                <p>  &#169;All rights reserved</p>
                            </div>
    </body>
</html>
